package com.atguigu.eduservice.service.impl;
import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.RealLandBase;
import com.atguigu.eduservice.entity.excel.BuildingExcel;
import com.atguigu.eduservice.entity.excel.LandExcel;
import com.atguigu.eduservice.listener.BuildingExcelListener;
import com.atguigu.eduservice.listener.LandExcelListener;
import com.atguigu.eduservice.mapper.RealLandBaseMapper;
import com.atguigu.eduservice.service.RealLandBaseService;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

@Component
public class RealLandBaseServiceImpl implements RealLandBaseService {

    @Resource
    RealLandBaseMapper realLandBaseMapper;


    @Override
    public void saveLandExcel(MultipartFile file, RealLandBaseService realLandBaseService ,RealLandBaseMapper realLandBaseMapper,String sheetNameVal) {



//            ExcelReaderBuilder excelReaderBuilder = EasyExcel.read(in);
//            ExcelReader excelReader = excelReaderBuilder.build();
//            List<ReadSheet> sheets = excelReader.excelExecutor().sheetList();
//            循环获取出所有sheet列
//            for (ReadSheet sheet : sheets) {
//                System.out.println("sheet" + sheet.getSheetName());
//                System.out.println("sheetNo"+sheet.getSheetNo());
//            }
            //ReadSheet{sheetNo=1, sheetName='土地数据收集模板'} com.alibaba.excel.read.metadata.ReadSheet@1de03052
            //ReadSheet{sheetNo=2, sheetName='房产数据收集模板'} com.alibaba.excel.read.metadata.ReadSheet@2e7b3106

            if(sheetNameVal.equals("土地数据收集模板")){
                try {
                    //文件输入流
                    InputStream in = file.getInputStream();
                    //调用方法进行读取
                    EasyExcel.read(in, LandExcel.class,new LandExcelListener(realLandBaseService,realLandBaseMapper))
                            //指定sheet页，sheetNo从0开始
                            .sheet(1)
                            // headRowNumber(1) 这里可以设置1，因为头就是一行。如果多行头，可以设置其他值。不传入也可以，他没有指定头，也就是默认1行
                            .headRowNumber(5)//5是从编号6开始读
                            .doRead();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }else if(sheetNameVal.equals("房产数据收集模板")){
                try {
                    //文件输入流
                    InputStream in = file.getInputStream();
                    //调用方法进行读取
                    EasyExcel.read(in, BuildingExcel.class,new BuildingExcelListener(realLandBaseMapper))
                            //指定sheet页，sheetNo从0开始
                            .sheet(2)
                            // headRowNumber(1) 这里可以设置1，因为头就是一行。如果多行头，可以设置其他值。不传入也可以，他没有指定头，也就是默认1行
                            .headRowNumber(5)//5是从编号6开始读
                            .doRead();
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
    }

    @Override
    public void insertBatch(List<RealLandBase> list) {
        realLandBaseMapper.insertBatch(list);

    }
}
